using GameFrameworkLibraly.BaseObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameFrameworkLibraly.Controls
{
    public class ImageControl:Control
    {
        private readonly bool _stratchToRectangle;

        /// <summary>
        /// Image that will be drawn
        /// </summary>
        public Texture2D Image { get; set; }

        /// <summary>
        /// Creates a new instance of ImageControl
        /// </summary>
        /// <param name="game">Current game</param>
        /// <param name="image">Image that will be drawn</param>
        /// <param name="destionationRectangle">Rectangle where image will be drawn</param>
        public ImageControl(MyGame game, Texture2D image, Rectangle destionationRectangle):base(game)
        {
            Image = image;
            DestinationRectangle = destionationRectangle;
        }

        /// <summary>
        /// Creates a new instance of ImageControl
        /// </summary>
        /// <param name="game">Current game</param>
        /// <param name="image">Image that will be drawn</param>
        /// <param name="destionationRectangle">Rectangle where image will be drawn</param>
        /// <param name="stratchToRectangle"></param>
        public ImageControl(MyGame game, Texture2D image, Rectangle destionationRectangle, bool stratchToRectangle):this(game,image,destionationRectangle)
        {
            _stratchToRectangle = stratchToRectangle;
        }

        #region Control

        /// <summary>
        /// Draws the control
        /// </summary>
        /// <param name="spriteBatch"></param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if(!Visible)
                return;
            if(BackgroundTexture!=null)
                spriteBatch.Draw(BackgroundTexture,DestinationRectangle,BackColorFilter);
            if(!_stratchToRectangle)
                spriteBatch.Draw(Image,DestinationRectangle,ForeColor);
            else
                spriteBatch.Draw(Image, new Vector2(DestinationRectangle.Left + (DestinationRectangle.Width - Image.Width) / 2, DestinationRectangle.Top + (DestinationRectangle.Height - Image.Height) / 2), ForeColor);
            
        }

        /// <summary>
        /// Updates state of the control
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            return;
        }

        #endregion
    }
}
